package com.estar.assistsurvy.receiver;

import com.estar.assistsurvy.activity.LoginActivity;
import com.estar.assistsurvy.activity.RoborderActivity;
import com.estar.assistsurvy.activity.TaskSuperviseActivity;
import com.estar.assistsurvy.controller.ActivityController;
import com.estar.assistsurvy.entity.NotifyMessage;
import com.estar.assistsurvy.entity.grabPondList;
import com.estar.assistsurvy.utils.VoiceUtil;
import com.google.gson.Gson;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "wl";
	private Gson gson=new Gson();
	private NotifyMessage notify=new NotifyMessage();
	private Bundle bundle;		
	private SharedPreferences prefer,nofityShare;
	private Intent inten;

	@Override
	public void onReceive(final Context context, Intent intent) {
		// TODO Auto-generated method stub
		nofityShare=context.getSharedPreferences("username", Context.MODE_PRIVATE);
		String phoneNumber=nofityShare.getString("telephone", "");
		
		bundle=intent.getExtras();
		notify=gson.fromJson(bundle.getString(JPushInterface.EXTRA_MESSAGE), NotifyMessage.class);
		//		type=notify.getType();
		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
			Log.i(TAG, "[MyReceiver] 接收Registration Id : " + regId);
			//send the Registration Id to your server...

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			Log.i(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
			prefer=context.getSharedPreferences("bundle", Context.MODE_PRIVATE);
			Editor editor=prefer.edit();
			editor.putString("notify", bundle.getString(JPushInterface.EXTRA_MESSAGE));
			editor.commit();
			
			
			final Activity activity = ActivityController.getCurrentActivity(); 
			if(notify.getType().equals("1")){
				new AlertDialog.Builder(activity,AlertDialog.THEME_HOLO_LIGHT)
				.setTitle("下线通知")
				.setMessage(notify.getMessage())
				.setCancelable(false)  
				.setPositiveButton("退出", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
//						activity.finish();
//						System.exit(0);
						Intent i=new Intent(context, LoginActivity.class);
						i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						activity.startActivity(i);
//						activity.finish();
					}
				})
				.setNegativeButton("重新登录", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Intent i=new Intent(context, LoginActivity.class);
						i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						activity.startActivity(i);
//						activity.finish();
					}
				})
				.create()
				.show();
				prefer=context.getSharedPreferences("user", Context.MODE_PRIVATE);
				prefer.edit().clear().commit();
//				intent=new Intent(context, MainActivity.class);
//				intent.putExtra("notify", notify);
//				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//				context.startActivity(intent);
			}else if(notify.getType().equals("2")){
				grabPondList grabPond=notify.getEsGrabPondInfo();
				String voice="距离您"+grabPond.getDistance()+"公里，"+grabPond.getOccurPlace()+"，有车出险，佣金"+grabPond.getPredictCharge()+"元，请抢单!";
				new VoiceUtil(voice, context).VoicePlay();
			}else if(notify.getType().equals("3")){
				String voice=notify.getMessage();
				new VoiceUtil(voice, context).VoicePlay();
				new AlertDialog.Builder(activity,AlertDialog.THEME_HOLO_LIGHT)
				.setTitle("案件跟进通知")
				.setMessage(notify.getMessage())
				.setCancelable(false)  
				.setPositiveButton("查看", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Intent i=new Intent(context, TaskSuperviseActivity.class);
						i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						activity.startActivity(i);
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				})
				.create()
				.show();
			}

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			Log.i(TAG, "[MyReceiver] 接收到推送下来的通知");
			int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			Log.i(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

			nofityShare=context.getSharedPreferences(phoneNumber, Context.MODE_PRIVATE);
			int i=nofityShare.getInt("number", 0);
			Log.i("nf", "接收器前推送消息数目---->"+i);
			i++;
			Log.i("nf", "接收器中推送消息数目---->"+i);
			Editor edit=nofityShare.edit();
			edit.putInt("number", i);
			edit.commit();
			Log.i("nf", "接收器后推送消息数目---->"+i);
			//            i++;
			//            brInteraction.setItem(i);

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			Log.i(TAG, "[MyReceiver] 用户点击打开了通知");
			prefer=context.getSharedPreferences("bundle", Context.MODE_PRIVATE);
			String bundles=prefer.getString("notify", "");
			notify=gson.fromJson(bundles, NotifyMessage.class);
			if(notify.getType().equals("1")){
				prefer=context.getSharedPreferences("user", Context.MODE_PRIVATE);
				prefer.edit().clear().commit();
				inten = new Intent(context, LoginActivity.class);
				inten.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				//			        	i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
				context.startActivity(inten);

			}else if(notify.getType().equals("2")){
				//打开自定义的Activity
				inten = new Intent(context, RoborderActivity.class);
				inten.putExtras(bundle);
				inten.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				//            	i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
				context.startActivity(inten);
			}

		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
			Log.i(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
			//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

		} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
			boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
			Log.i(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
		} else {
			Log.i(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
		}
	}

//	private BRInteraction brInteraction;
//	
//		public interface BRInteraction {
//	        public void setCode(String code);
//	    }
//	
//	    public void setBRInteractionListener(BRInteraction brInteraction) {
//	        this.brInteraction = brInteraction;
//	    }

}
